package com.ysh.smartplatform.service.impl;

import com.ysh.smartplatform.cache.MenuCache;
import com.ysh.smartplatform.cache.RoleCache;
import com.ysh.smartplatform.constans.ResultCode;
import com.ysh.smartplatform.event.MenuChangeEvent;
import com.ysh.smartplatform.exceptions.AddDataException;
import com.ysh.smartplatform.mapper.MenuMapper;
import com.ysh.smartplatform.pojo.*;
import com.ysh.smartplatform.service.MenuService;
import com.ysh.smartplatform.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 袁胜虎 on 2022/9/16 20:27
 */
@Service
@Transactional
public class MenuServiceImpl implements MenuService {
    private ApplicationContext context;

    @Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }


    private RoleCache roleCache;


    @Autowired
    public void setRoleCache(RoleCache roleCache) {
        this.roleCache = roleCache;
    }

    private MenuMapper menuMapper;

    @Autowired
    public void setMenuMapper(MenuMapper menuMapper) {
        this.menuMapper = menuMapper;
    }

    private MenuCache menuCache;

    @Autowired
    public void setMenuCache(MenuCache menuCache) {
        this.menuCache = menuCache;
    }

    @Override
    public void addMenu(Menu menu) {
        Assert.isTrue(menu!=null&&!menu.isEmpty(CheckType.ADD), ()->{
            throw new AddDataException("含有空的参数", ResultCode.DATA_NOT_EXIST);
        });
        //判断名字或者地址是否已经存在
        MenuExample menuExample = new MenuExample();
        menuExample.createCriteria().andMenuNameEqualTo(menu.getMenuName());
        List<Menu> menuList = menuMapper.selectByExample(menuExample);
        //根据url查询地址
        List<Menu> menuListByUrl = null;
        if (StringUtils.hasText(menu.getUrl())) {
            menuExample.clear();
            menuExample.createCriteria().andUrlEqualTo(menu.getUrl());
            menuListByUrl=menuMapper.selectByExample(menuExample);
        }

        Assert.isTrue((menuList==null||menuList.size()==0)&&(menuListByUrl==null||menuListByUrl.size()==0),()->{
            throw  new AddDataException("数据为空",ResultCode.DATA_NOT_EXIST);
        });
        BaseUser user = SecurityUtils.getLoginUser();
        Assert.notNull(user, () -> {
            throw new AddDataException("用户未登录",ResultCode.USERNAME_ALREADY_EXIST);
        });
        menu.setCreateTime(new Date());
        menu.setCreateBy(user.getUsername());
        menuMapper.insertSelective(menu);
        //发布事件,更新缓存
        context.publishEvent(new MenuChangeEvent());
    }

    @Override
    public void updateMenu(Menu menu) {
        BaseUser user = SecurityUtils.getLoginUser();
        //设置更新操作的用户信息
        menu.setUpdateBy(user.getUsername());
        menu.setUpdateTime(new Date());
        menuMapper.updateByPrimaryKeySelective(menu);
        //发布事件,更新缓存
        context.publishEvent(new MenuChangeEvent());
    }

    @Override
    public List<Menu> findAllMenus() {
        List<Menu> menuList = menuCache.getAllData();
        return menuList;
    }

    @Override
    public List<Menu> findAllEnableMenus() {
        //从缓存中查询数据
        List<Menu> menuList = menuCache.getEnableList();
        return menuList;
    }

    @Override
    public List<Menu> findAllMenusByRoleId(Long roleId) {

        //通过缓存获取到角色信息
        Role role = roleCache.get(roleId);
        //获取到角色的菜单信息
        List<Menu> menuList = role.getMenuList();
        return menuList;
    }

    @Override
    public Menu getMenuById(Long id) {
        Menu menu = menuCache.getEnableMenu(id);
        return menu;
    }

    @Override
    public void deleteById(Long id) {
        menuMapper.deleteById(id);
        context.publishEvent(new MenuChangeEvent());
    }
}
